/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SPEx.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.core;

import android.text.TextUtils;

import java.util.ArrayList;

import io.bhex.baselib.constant.AppData;
import io.bhex.baselib.network.Utils.Convert;
import io.bhex.baselib.utils.DebugLog;
import io.bhex.baselib.utils.SP;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.utils.bean.MsgListBean;

/**
 * 主要是对原来存储的数据做一个兼容，原来所有的数据都是通过String存储的
 * ================================================
 */
public class SPEx extends SP {

    public static int get(String key, int def) {
        try {
            return SP.get(key, def);
        } catch (ClassCastException e) {
            String result = SP.get(key, def + "");
            if (TextUtils.isEmpty(result))
                return def;

            return Integer.parseInt(result);
        } catch (Exception e) {
            DebugLog.e(e);
            return def;
        }
    }

    public static boolean get(String key, boolean def) {
        try {
            return SP.get(key, def);
        } catch (ClassCastException e) {
            String result = SP.get(key, def + "");
            if (TextUtils.isEmpty(result))
                return def;

            return Boolean.parseBoolean(result);
        } catch (Exception e) {
            DebugLog.e(e);
            return def;
        }
    }

    public static String get(String key, String def) {
        try {
            return SP.get(key, def);
        } catch (Exception e) {
            DebugLog.e(e);
            return def;
        }
    }

    public static float get(String key, float def) {
        try {
            return SP.get(key, def);
        } catch (ClassCastException e) {
            String result = SP.get(key, def + "");
            if (TextUtils.isEmpty(result))
                return def;

            return Float.parseFloat(result);
        } catch (Exception e) {
            DebugLog.e(e);
            return def;
        }
    }

    public static long get(String key, long def) {
        try {
            return SP.get(key, def);
        } catch (ClassCastException e) {
            String result = SP.get(key, def + "");
            if (TextUtils.isEmpty(result))
                return def;

            return Long.parseLong(result);
        } catch (Exception e) {
            DebugLog.e(e);
            return def;
        }
    }

    public static void setFingerOpen(boolean isOpen){
        SPEx.set(AppData.SPKEY.FINGER_PWD_KEY+UserInfo.getUserId(),isOpen);
    }

    public static boolean isFingerOpen(){
       return SPEx.get(AppData.SPKEY.FINGER_PWD_KEY+UserInfo.getUserId(),false);
    }

    public static void removeFingerOpen(){
       SPEx.remove(AppData.SPKEY.FINGER_PWD_KEY+UserInfo.getUserId());
    }

    public static void setFingerTip(boolean isOpen){
        SPEx.set(AppData.SPKEY.FINGER_tip_KEY+UserInfo.getUserId(),isOpen);
    }

    public static boolean isFingerTip(){
        return SPEx.get(AppData.SPKEY.FINGER_tip_KEY+UserInfo.getUserId(),false);
    }

    public static void removeFingerTip(){
        SPEx.remove(AppData.SPKEY.FINGER_tip_KEY+UserInfo.getUserId());
    }


    public static void setFingerAuth(boolean isAuth){
//        DebugLog.e(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId()+"    Set =====    "+isAuth);
        SPEx.set(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId(),isAuth);
    }

    public static boolean isFingerAuth(){
//        DebugLog.e(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId()+"    Is =====    "+SPEx.get(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId(),false));
       return SPEx.get(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId(),false);
    }

    public static void removeFingerAuth(){
       SPEx.remove(AppData.SPKEY.USER_AUTH_FINGER+UserInfo.getUserId());
    }

    /**
     * 获取该订单的已读消息id
     * @param orderId
     * @return
     */
    public static String getMsgId(String orderId) {
        return SP.get(orderId,"0");
    }

    /**
     * 根据订单号保存消息id
     * @param orderId
     * @param id
     */
    public static void setMsgId(String orderId, String id) {
        boolean isHasSave = !getMsgId(orderId).equals("0");
        SP.set(orderId,id);
        try{

            String msgIdList = SP.get("msgIdList", "");
            MsgListBean msgListBean;
            ArrayList<String> data;
            if (!TextUtils.isEmpty(msgIdList)) {
                msgListBean = Convert.fromJson(msgIdList, MsgListBean.class);
                if (msgListBean == null) {
                    msgListBean=new MsgListBean();
                    data = new ArrayList<String>();
                    msgListBean.setArray(data);
                }else{
                    data = msgListBean.getArray();
                }
                if (data.size()>50) {
                    for (int length = data.size(); length > 0; length--) {
                        if (length>50) {
                            boolean remove = SP.removeExt(data.get(length-1));
                            if (remove) {
                                data.remove(length-1);
                            }
                        }
                    }
                }
            }else{
                msgListBean = new MsgListBean();
                data = new ArrayList<String>();
                msgListBean.setArray(data);
            }
            if (!isHasSave) {
                //不存在，再存储id
                data.add(0,orderId);
            }
            SP.set("msgIdList",Convert.toJson(msgListBean));

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
